﻿using Helper;
using Helper.Models;
using Starehe.Views;
using System;
using System.Security.Permissions;
using System.Windows;
using System.Windows.Threading;
namespace Starehe
{
    public partial class App : Application, IApp
    {
        UserRoles currentUserRole;
        public App()
        {
            currentUserRole = UserRoles.Guest;
        }

        public UserRoles CurrentUserRole
        {
            get { return currentUserRole; }
        }

        [ObjectPermissionAttribute(SecurityAction.Demand, RequiredPermissionAccess = UserRoles.Administrator)]
        internal void SetUserRole(UserRoles role)
        {
            currentUserRole = role;
        }



        private void InitGlobalVar()
        {
            if (string.IsNullOrWhiteSpace(Helper.Properties.Settings.Default.ServerName))
                Helper.Properties.Settings.Default.ServerName = Environment.MachineName +
                    "\\Starehe";
            if (string.IsNullOrWhiteSpace(Helper.Properties.Settings.Default.DBName))
                Helper.Properties.Settings.Default.DBName = "Starehe";
            if (string.IsNullOrWhiteSpace(Helper.Properties.Settings.Default.MostRecentBackup))
                Helper.Properties.Settings.Default.MostRecentBackup = "";
            
            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-GB");
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en-GB");

            Helper.Properties.Settings.Default.PropertyChanged += (o, e) =>
                {
                    Helper.Properties.Settings.Default.Save();
                };
        }

        private StartUp StartUpCheck()
        {
            if ((SystemParameters.PrimaryScreenHeight < 768) || (SystemParameters.PrimaryScreenWidth < 1024))
            {
                MessageBox.Show("The screen resolution is too low.\r\nSome elements may not display properly." +
                    "\r\nPlease configure the Display setting" +
                    " to a resolution equal to or higher than 1024 x 768.", "Error",
                    MessageBoxButton.OK, MessageBoxImage.Error);
            }

            StartUp start = new StartUp();
            FileHelper.CheckFiles();
            if (Helper.Properties.Settings.Default.UseDefaultSQLInstance)
                start.IsSQLInstalled = DatabaseDefn.CheckSQLInstalled() && DatabaseDefn.CheckInstanceInstalled();
            else
            {
                start.IsSQLInstalled = DatabaseDefn.CheckSQLInstalled();
                start.IsSQLInstanceInstalled = DatabaseDefn.CheckInstanceInstalled();
            }
            start.IsDefaultLoginOK = DatabaseDefn.CheckDefaultLogin();
            start.IsDbOK = DatabaseDefn.CheckDB();

            if (Helper.Properties.Settings.Default.DoFullStartupCheck)
                start.IsDbObjectsOK = DatabaseDefn.CheckDBObjects();
            else
                start.IsDbObjectsOK = true;
            if (!start.IsSQLInstalled)
            {
                start.IsSQLInstanceInstalled = false;
                start.IsDefaultLoginOK = false;
                start.IsDbOK = false;
                start.IsDbObjectsOK = false;
                return start;
            }

            if (!start.IsSQLInstanceInstalled)
            {
                start.IsDefaultLoginOK = false;
                start.IsDbOK = false;
                start.IsDbObjectsOK = false;
                return start;
            }

            if (!start.IsDefaultLoginOK)
            {
                start.IsDbOK = false;
                start.IsDbObjectsOK = false;
                return start;
            }

            if (!start.IsDbOK)
            {
                start.IsDbObjectsOK = false;
                return start;
            }

            return start;
        }

        private void Application_Startup(object sender, StartupEventArgs e)
        {
            InitGlobalVar();
            StartUp start = StartUpCheck();
            if (!start.IsOK)
            new StartupRepair(StartUpCheck()).ShowDialog(); 

            try
            {
                LoginWindow lg = new LoginWindow();
                lg.ShowDialog();
            }
            catch { }
            
        }
        
        private void Application_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {                       
            e.Handled = true;            
        }

        private void Application_Exit(object sender, ExitEventArgs e)
        {
        }

        private void Application_SessionEnding(object sender, SessionEndingCancelEventArgs e)
        {        
        }        
    }
}
